<template>
	<view>
		<view class="weather-top-bar">
			<text class="temp">{{ weatherData.temp }}°C</text>
			<text class="desc">{{ weatherData.weather }}</text>
			<text class="city">泉州</text>
			<text class="pm">PM2.5: {{ weatherData.pm }}</text>
		</view>
		<view v-for="(news, index) in newsList" :key="index" style="margin: 10rpx;">
			<CustomTextView :title="news.title" :is-top="news.isTop" :author="news.author" :comments="news.comments"
				:time="news.time" :image="news.imagelist && news.imagelist.length > 0 ? news.imagelist[0] : ''"
				:ad-image-list="news.adImageList" :mode="news.isTop ? 1 : 2" :is-ad="news.isAd"></CustomTextView>
		</view>
	</view>
</template>

<script>
	import newsList from '../../Data/news.json'
	import CustomTextView from '../../components/cardViewText.vue'

	export default {
		components: {
			CustomTextView
		},
		data() {
			return {
				newsList: newsList,
				weatherData: {
					temp: "24",
					weather: "多云",
					pm: "42"
				},
				weatherURL: "http://t.weather.sojson.com/api/weather/city/101230505",
			}
		},
		onMounted() {
			this.getWeather()
		},
		methods: {
			getWeather() {
				uni.request({
					url: this.weatherURL,
					method: "GET",
					success: (res) => {
						if (res.statusCode === 200 && res.data?.status === 200) {
							const today = res.data.data.forecast[0]
							this.weatherData = {
								temp: today.high.replace('℃', ''),
								weather: today.type,
								pm: res.data.data.pm25
							}
						}
					}
				})
			}
		}
	}
</script>

<style>
	.weather-top-bar {
		background-color: #ff6b35;
		color: #fff;
		padding: 15rpx 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.temp {
		font-size: 36rpx;
		font-weight: bold;
	}

	.desc,
	.city,
	.pm {
		font-size: 26rpx;
		margin-left: 15rpx;
	}
</style>